# l1 = [1, 2, 3]
# ret = str(l1)
# print(ret,type(ret))
# print(eval(ret))  # 不让用

# 文件存取时,遇到的矛盾.
# dic ={'username': '太白', 'password': 123}

# dic = {1: {'username': '太白', 'password': 123,'status': False},
#        2:{'username': 'alex', 'password': 123,'status': False}
#        }

# 这个字典能放在全局么?
# with open('register.json',encoding='utf-8',mode='w') as f1:
#     f1.write(str(dic))
# with open('register',encoding='utf-8') as f1:
#     ret = f1.read()
#     print(ret,type(ret))
#
# # 数据结构 --- > str() 存储在文件, 读取出来时,反转不回去.
#
#
# # 网络传输.
# l1 = [i for i in range(100000)]
# # 凡是数据通过网络传出去最终的格式必须bytes
# s1 = str(l1)
# b1 = s1.encode('utf-8')
# print(b1)  # b1可以发送出去
#
# s2 = b1.decode('utf-8')
# print(s2,type(s2))
# s2 转化不成列表了.

# 我们现在要解决的问题: 如果有一种特殊的字符串,这个字符串可以与任何的数据结构互相转换.

# 序列化模块: 将一中数据结构转化成特殊的序列(特殊的字符串,bytes)并且还可以反转回去.

    # json模块: 是所有语言公认的一种序列.最最常用的
    #所以支持的python数据结构有限: int str bool dict list(tuple),None,float

    # None  ---> Null ----> NUll
    # dict  --->
    # pickle模块: 只能python语言中使用的,序列化模块:
    # 支持python所有的数据类型以及对象.
    # shevle模块(不讲): 课下了解(只能是文件存取).

# Json序列化
# 两对四个方法.
import json
# dumps,loads 主要用于网络传输,可以用于文件的存取.
# dic = {'username': '太白', 'password': 123,'status': False}
# ret = json.dumps(dic)
# # print(ret,type(ret))

# ret_dict = json.loads(ret)
# print(ret_dict)

# 特殊的参数
# dic = {'username': '太白', 'password': 123,'status': False}
# # print(dic)
# ret = json.dumps(dic,ensure_ascii=False,sort_keys=True)
# print(ret,type(ret))
# import json
# dic = {'username': '太白', 'password': 123,'status': False}
# s_dict = json.dumps(dic)
# with open('jsonlx.json',encoding='utf-8',mode='w') as f1:
#     f1.write(s_dict)
#
# with open('jsonlx.json',encoding='utf-8') as f2:
#     content = f2.read()
#     print(json.loads(content))

# dump load: 单个数据的存取文件.
# import json
# dic = {'username': '太白', 'password': 123,'status': False}
# with open('jsonlx1.json',encoding='utf-8',mode='w') as f1:
#     json.dump(dic,f1)
#
#
# with open('jsonlx1.json',encoding='utf-8') as f1:
#     dic1 = json.load(f1)
#
# print(dic1,type(dic1))


# 多个数据如何存储到一个文件中?
# 错误演示:
# import json
# dic1 = {'username': '太白', 'password': 123,'status': False}
# dic2 = {'username': 'alex', 'password': 123,'status': False}
# dic3 = {'username': 'ly', 'password': 123,'status': False}
# with open('jsonmore.json',encoding='utf-8',mode='a') as f1:
#     # f1.write(json.dumps(dic1))
#     # f1.write(json.dumps(dic2))
#     # f1.write(json.dumps(dic3))
#     f1.write(f'{json.dumps(dic1)}{json.dumps(dic2)}{json.dumps(dic3)}')

#
# with open('jsonmore.json',encoding='utf-8') as f1:
#     ret = json.loads(f1.read())
#     print(ret)

# 正确做法:

import json
# dic1 = {'username': '太白', 'password': 123,'status': False}
# dic2 = {'username': 'alex', 'password': 123,'status': False}
# dic3 = {'username': 'ly', 'password': 123,'status': False}
# with open('jsonmore.json',encoding='utf-8',mode='a') as f1:
#     f1.write(f'{json.dumps(dic1)}\n{json.dumps(dic2)}\n{json.dumps(dic3)}')

# with open('jsonmore.json',encoding='utf-8') as f1:
#     for line in f1:
#         ret = json.loads(line)
#         print(ret,type(ret))


# pickle
import pickle

# dumps,loads  只能是网络传输
# l1 = ['wusir', '太白', '小黑', 666]
# ret = pickle.dumps(l1)
# # print(ret)
#
# l2 = pickle.loads(ret)
# print(l2,type(l2))

# dump load 数据结构存取文件.
import pickle
# l1 = ['wusir', '太白', '小黑', 666]
# with open('pickle练习.pickle',mode='wb') as f1:
#     pickle.dump(l1,f1)

# with open('pickle练习.pickle', mode='rb') as f1:
#     ret = pickle.load(f1)
#     print(ret,type(ret))

# 多个数据写入文件
l1 = ['wusir', '太白', '小黑1', 666]
l2 = ['wusir', '太白', '小黑2', 666]
l3 = ['wusir', '太白', '小黑3', 666]
# with open('pickle练习1.pickle',mode='wb') as f1:
# #     pickle.dump(l1,f1)
# #     pickle.dump(l2,f1)
# #     pickle.dump(l3,f1)
#
# with open('pickle练习1.pickle', mode='rb') as f1:
#     ret1 = pickle.load(f1)
#     ret2 = pickle.load(f1)
#     ret3 = pickle.load(f1)
#     print(ret1,ret2,ret3)

# 总结 